package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.tsktask.TskTaskPo;
import com.bwda.dsrs.filecenter.domain.condition.tsktask.TskTaskCondition;
import com.bwda.dsrs.filecenter.domain.form.tsktask.TskTaskQueryForm;
import com.bwda.dsrs.filecenter.domain.form.tsktask.TskTaskUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.tsktask.TskTaskCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.tsktask.TskTaskVo;
import com.bwda.dsrs.filecenter.service.TskTaskService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/tsktask")
@Api(value = "/tsktask", tags = "任务表")
public class TskTaskController extends BaseController {

	@Autowired
	private TskTaskService tskTaskService;

	@ApiOperation(value = "查询任务表",notes = "查询任务表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<TskTaskVo> query(@ApiParam(value = "任务id", required = true)@RequestParam Integer taskId) throws BusinessServiceException {
		TskTaskPo po = tskTaskService.queryWithValid(taskId);
		TskTaskVo vo = CopyUtil.transfer(po, TskTaskVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询任务表数量",notes = "查询任务表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid TskTaskQueryForm form) throws BusinessServiceException {
		TskTaskCondition condition = this.getConditionByQueryForm(form);
		int count = tskTaskService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询任务表列表",notes = "查询任务表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<TskTaskVo>> queryList(@ModelAttribute@Valid TskTaskQueryForm form) throws BusinessServiceException {
		TskTaskCondition condition = this.getConditionByQueryForm(form);
		List<TskTaskPo> poList = tskTaskService.queryList(condition);
		List<TskTaskVo> voList = CopyUtil.transfer(poList, TskTaskVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询任务表列表(带分页)",notes = "查询任务表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<TskTaskVo>> queryPageList(@ModelAttribute@Valid TskTaskQueryForm form) throws BusinessServiceException {
		TskTaskCondition condition = this.getConditionByQueryForm(form);
		List<TskTaskVo> voList = new ArrayList<>();
		int count = tskTaskService.queryCount(condition);
		if (count > 0) {
			List<TskTaskPo> poList = tskTaskService.queryList(condition);
			voList = CopyUtil.transfer(poList, TskTaskVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}


	@ApiOperation(value = "修改任务表",notes = "修改任务表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid TskTaskUpdateForm form) throws BusinessServiceException {
		TskTaskPo po = CopyUtil.transfer(form, TskTaskPo.class);
		tskTaskService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除任务表",notes = "删除任务表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "任务id", required = true)@RequestParam Integer taskId) throws BusinessServiceException {
		tskTaskService.delete(taskId);
		return getSuccessResult();
	}

	/**
	 * TskTaskQueryForm转换为TskTaskCondition
	 *
	 * @param form
	 * @return
	 */
	private TskTaskCondition getConditionByQueryForm(TskTaskQueryForm form) {
		TskTaskCondition condition = CopyUtil.transfer(form, TskTaskCondition.class);
		return condition;
	}

}